﻿using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.List.PlugIn;
using Kingdee.BOS.Core.List.PlugIn.Args;
using Kingdee.BOS.Util;
using System;
using System.ComponentModel;
using System.Linq;

namespace Caraya.MFG.BusinessPlugIn
{
    [Description("生产用料清单列表插件测试"), HotUpdate]
    public class PRD_PPBOM_List : AbstractListPlugIn
    {
        private bool ReadUserParameter()
        {
            this.Model.ParameterData.TryGetValue("FFDELIVERYDATE", out var FFDELIVERYDATE);
            if (FFDELIVERYDATE != null && FFDELIVERYDATE.ToString().Equals("true", StringComparison.OrdinalIgnoreCase))
            {
                return true;
            }
            return false;
        }
        public override void PrepareFilterParameter(FilterArgs e)
        {
            base.PrepareFilterParameter(e);
            if (!ReadUserParameter())
            {
                var fields = e.ColumnFields;
                if (fields == null)
                {
                    return;
                }
                this.ListModel.FilterParameter.ColumnInfo.Remove(fields.Where(t => t.FieldName == "FREQSRC").First());
                this.ListModel.FilterParameter.ColumnInfo.Remove(fields.Where(t => t.FieldName == "FSALEORDERENTRYID").First());
            }
        }
        public override void CreateListHeader(CreateListHeaderEventArgs e)
        {
            base.CreateListHeader(e);
            if (ReadUserParameter())
            {
                var header = e.ListHeader.AddChild();
                header.Caption = new LocaleValue("销售要货日期");
                header.Key = "FDynamicColumn1";
                header.FieldName = "FDynamicColumn1";
                header.ColType = SqlStorageType.Sqlnvarchar;
                header.Width = 200;
                header.Visible = true;
                header.ColIndex = e.ListHeader.GetChilds().Max(o => o.ColIndex) + 1;
            }

        }

        public override void FormatCellValue(FormatCellValueArgs args)
        {
            base.FormatCellValue(args);
            if (ReadUserParameter())
            {
                if (args.Header.Key.Equals("FDynamicColumn1", StringComparison.OrdinalIgnoreCase))
                {
                    var obj = args.DataRow as DynamicObjectDataRow;
                    var row = obj.DynamicObject;
                    row.TryGetValue("FReqSrc", out var src);
                    if (src != null)
                    {
                        if (src.ToString().Equals("1", StringComparison.OrdinalIgnoreCase))
                        {
                            var FSALEORDERENTRYID = row["FSALEORDERENTRYID"];
                            var strSQL = $"SELECT FDELIVERYDATE FROM T_SAL_ORDERENTRYDELIPLAN WHERE (FENTRYID = {FSALEORDERENTRYID})";
                            var dt = DBUtils.ExecuteScalar<DateTime?>(this.Context, strSQL, null);
                            args.FormateValue = string.Format("{0:yyyy-MM-dd}", dt);
                        }
                    }
                }
            }

        }
    }
}
